import { check, checkError, checkResultPage, getRequest } from "./baseUtils"

// 待支付订单
export class WaitPaidOrderApi {
	
	static baseUrl = "/order/wait_paid"
	
	// 分页获取待支付订单
	static async getWaitPayOrders(page) {
		uni.showLoading({title: "获取中", mask: true})
		try {
			const response = await uni.request(getRequest({
				url: this.baseUrl,
				method: "GET",
				data: page
			}))
			return checkResultPage(response)
		}
		catch (error) { checkError(error) }
		finally { uni.hideLoading() }
	}
	
	// 创建一个待支付订单
	static async createWaitPayOrder(order) {
		uni.showLoading({title: "创建订单", mask: true})
		try {
			const response = await uni.request(getRequest({
				url: this.baseUrl,
				method: "POST",
				data: order
			}))
			return check(response)
		}
		catch (error) { checkError(error) }
		finally { uni.hideLoading() }
	}
	
	// 取消一个待支付订单
	static async cancelWaitPayOrder(id) {
		uni.showLoading({title: "创建订单", mask: true})
		try {
			const response = await uni.request(getRequest({
				url: this.baseUrl + "/" + id,
				method: "DELETE"
			}))
			return check(response)
		}
		catch (error) { checkError(error) }
		finally { uni.hideLoading() }
	}
	
	// 为待支付订单进行付款
	static async payWaitPayOrder(id) {
		uni.showLoading({title: "支付中", mask: true})
		try {
			const response = await uni.request(getRequest({
				url: this.baseUrl + "/" + id,
				method: "PUT"
			}))
			return check(response)
		}
		catch (error) { checkError(error) }
		finally { uni.hideLoading() }
	}
}

// 待发货订单
export class WaitDeliveryOrderApi {
	
	static baseUrl = "/order/wait_delivery"
	
	// 分页获取待发货订单
	static async getWaitDeliveryOrders(page) {
		uni.showLoading({title: "获取中", mask: true})
		try {
			const response = await uni.request(getRequest({
				url: this.baseUrl,
				method: "GET",
				data: page
			}))
			return checkResultPage(response)
		}
		catch (error) { checkError(error) }
		finally { uni.hideLoading() }
	}
	
}

// 配送中订单
export class WaitReceivingOrderApi {
	
	static baseUrl = "/order/wait_receiving"
	
	// 分页获取待支付订单
	static async getWaitReceivingOrders(page) {
		uni.showLoading({title: "获取中", mask: true})
		try {
			const response = await uni.request(getRequest({
				url: this.baseUrl,
				method: "GET",
				data: page
			}))
			return checkResultPage(response)
		}
		catch (error) { checkError(error) }
		finally { uni.hideLoading() }
	}
	
	// 确认收货
	static async confirmReceivingOrder(id) {
		uni.showLoading({title: "获取中", mask: true})
		try {
			const response = await uni.request(getRequest({
				url: this.baseUrl + "/" + id,
				method: "PUT",
			}))
			return check(response)
		}
		catch (error) { checkError(error) }
		finally { uni.hideLoading() }
	}
	
}

// 已完成订单
export class CompletedOrderApi {
	
	static baseUrl = "/order/completed"
	
	// 分页获取待支付订单
	static async getCompletedOrders(page) {
		uni.showLoading({title: "获取中", mask: true})
		try {
			const response = await uni.request(getRequest({
				url: this.baseUrl,
				method: "GET",
				data: page
			}))
			return checkResultPage(response)
		}
		catch (error) { checkError(error) }
		finally { uni.hideLoading() }
	}
	
}